<template>
  <div class="login">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">通用后台管理系统</h3>
      <el-form-item prop="username">
        <el-input
          prefix-icon="iconfont icon-user"
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          prefix-icon="iconfont icon-lock"
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          prefix-icon="iconfont icon-security"
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCodeImg, login } from "@/api/login";
export default {
  name: "Login",
  data() {
    return {
      loading: false,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "123456",
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          try {
            this.loading = true;
            let res = await login(this.loginForm);
            this.loading = false;
            if (res.code === 200) {
              // 存储token
              localStorage.setItem("Authorization", res.token);
              // 跳转
              this.$router.replace("/index");
            } else {
              this.$message.error(res.msg);
              this.getCode();
            }
          } catch (e) {
            this.$message.success("error");
            this.loading = false;
            this.$message.error("登录失败");
          }
        }
      });
    },
  },
};
</script>


<style scoped lang="less">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 800px;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.login-form {
  width: 350px;
  height: 300px;
  border-radius: 2%;
  padding: 20px 40px;
  background-color: #fff;

  .login-code {
    width: 33%;
    float: right;
    .login-code-img {
      width: 100%;
      height: 38px;
      cursor: pointer;
      vertical-align: middle;
    }
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
</style>
